<template>
    <div class="common-page">
        <button :class="['iconfont icon-zuo', 'prev', { disable: fDisable }]" @click="handleClickArrow(-1)" />
        <a :href="getHref({ page: item })" v-for="item in active_page_arr" :key="item" :class="{ active: item === current }" @click.prevent="ChangePageNum(item)">
            {{ item }}
        </a>
        <button :class="['iconfont icon-you', 'next', { disable: lDisable }]" @click="handleClickArrow(1)" />
    </div>
</template>
<script>
export default {
    name: "CommonPage",
    props: {
        pagerCount: {
            type: Number,
            default: 5,
        },
        total: {
            type: Number,
            default: 0,
        },
        currentPage: {
            type: Number,
            default: 1,
        },
        pageSize: {
            type: Number,
            default: 0,
        },
    },
    data () {
        return {
            current: this.currentPage,
            page_size: this.pageSize,
        };
    },
    computed: {
        //页码数
        page: {
            get () {
                return Math.ceil(this.total / this.page_size) || 1;
            },
        },
        //页码数数组（排序首尾）
        page_arr: {
            get () {
                let arr = [];
                for (let i = 2; i < this.page; i++) {
                    arr.push(i);
                }
                return arr;
            },
        },
        //选中的current附近的数
        active_page_arr: {
            get () {
                let page = this.page;
                let limit = this.pagerCount;
                let current = this.current;
                let arr = [];
                if (page > limit) {
                    if (current < 4) {
                        for (let i = 1; i <= limit; i++) {
                            arr.push(i);
                        }
                    } else if (page - current <= 1) {
                        for (let i = page - limit + 1; i <= page; ++i) {
                            arr.push(i);
                        }
                    } else if (current >= 4) {
                        let count = parseInt(limit / 2); // 2
                        let last = current + count; // 4+2
                        if (last <= page) {
                            for (
                                let i = current - count;
                                i <= current + count;
                                i++
                            ) {
                                arr.push(i);
                            }
                        }
                    }
                } else {
                    for (let i = 1; i <= page; i++) {
                        arr.push(i);
                    }
                }
                // if (current < limit) {
                //   for (let i = 2; i <= limit && i < page; i++) {
                //     arr.push(i);
                //   }
                // } else if (current > max_count) {
                //   for (let i = max_count; i < page; i++) {
                //     arr.push(i);
                //   }
                // } else {
                //   let count = parseInt(limit / 2);
                //   let min = current - count;
                //   let max = current + count;
                //   for (let i = min; i <= max; i++) {
                //     arr.push(i);
                //   }
                // }
                return arr;
            },
        },
        fMore: {
            get () {
                return this.page > this.pagerCount ? this.current > 3 : false;
            },
        },
        lMore: {
            get () {
                return this.page > this.pagerCount
                    ? this.page - this.current > 2
                    : false;
            },
        },
        fDisable: {
            get () {
                if (this.current == 1) {
                    return true;
                }
                return false;
            },
        },
        lDisable: {
            get () {
                if (this.current == this.page) {
                    return true;
                }
                return false;
            },
        },
    },
    watch: {
        currentPage: {
            handler: function (v) {
                this.current = v;
            },
        },
        current: {
            handler: function (v) {
                if (v != this.currentPage) {
                    this.$emit("current-change", v);
                }
            },
            immediate: true,
            deep: true,
        },
    },
    methods: {
        handleClickArrow (val) {
            let result = this.current + val;
            if (result <= 0) {
                result = 1;
            }
            if (result > this.page) {
                if (this.lDisable) {
                    return;
                }
                result = this.page;
            }
            this.ChangePageNum(result);
        },
        ChangePageNum (val) {
            if (this.current == val) {
                return;
            }
            this.current = val;
            // let query = JSON.parse(JSON.stringify(this.$route.query));
            // query.page = val;
            // this.$router.push({ query: query });
        },

        //获取路由query信息
        getHref (obj) {
            let query = JSON.parse(JSON.stringify(this.$route.query)) || {};
            let arr = [];
            Object.keys(obj).map((key) => {
                query[key] = obj[key];
            });
            Object.keys(query).map((key) => {
                arr.push(key + "=" + query[key]);
            });
            return "?" + arr.join("&");
        },
    },
};
</script>
<style lang="scss" scoped>
.common-page {
    display: flex;
    flex-direction: row;
    align-items: center;
    overflow: hidden;
    & > a,
    & > button {
        display: flex;
        align-items: center;
        justify-content: center;
        user-select: none;
        margin-left: 12px;
        padding: 0;
        width: 28px;
        height: 28px;
        border-radius: 4px;
        box-sizing: border-box;
        border: none;
        font-size: 14px;
        font-family: Roboto-Regular, Roboto;
        font-weight: 400;
        color: #555555;
        transition: all 0.2s;

        &:hover {
            background-color: rgba(0, 0, 0, 0.05);
            color: #000;
        }
        &.active {
            border-color: #ea4c89;
            background: #ea4c89;
            color: #ffffff;
            cursor: default;
        }
        &.disable {
            color: #c6c6c6;
            border-color: #c6c6c6;
            cursor: default;
        }
        &:first-child {
            margin-left: 0;
        }
        &:last-child {
            margin-right: 0;
        }
        cursor: pointer;

        &.more {
            position: relative;
            span {
                position: absolute;
                top: 50%;
                left: 50%;
                transition: opacity 0.2s;
                transform: translate(-50%, -50%);
                &:nth-child(1) {
                    font-size: 20px;
                    font-weight: 600;
                    opacity: 1;
                }
                &:nth-child(2) {
                    font-size: 12px;
                    font-weight: 400;
                    opacity: 0;
                }
            }
            &:hover {
                span {
                    &:nth-child(1) {
                        opacity: 0;
                    }
                    &:nth-child(2) {
                        opacity: 1;
                    }
                }
            }
        }
    }
    &[type='small'] {
        > a,
        button {
            box-sizing: border-box;
            width: 24px;
            height: 24px;
            font-size: 12px;
            margin: 0 4px;
            &:first-child {
                margin-left: 0;
            }
            &:last-child {
                margin-right: 0;
            }
        }
        .more {
            span {
                &:nth-child(1) {
                    font-size: 14px;
                }
                &:nth-child(2) {
                    zoom: 0.7;
                }
            }
        }
    }
}
</style>
